package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @PostMapping("list")
    public Map<String ,Object> list(Integer page, Integer rows, String  customerName){
        List<Customer> list = customerService.selectList(page,rows,customerName);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        map.put("total",list.size());
        return map;
    }

    @PostMapping("save")
    public ServiceVO saveOrUpdate(Customer customer, HttpServletRequest request){
        String customerId = request.getParameter("customerId");
        Boolean flag = customerService.saveOrUpdate(customer,customerId);
        if (flag){
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO(200,"请求失败");
    }

    @PostMapping("delete")
    public ServiceVO delete(String ids){
        Boolean flag = customerService.delete(ids);
        if (flag){
            return new ServiceVO(100,"请求成功");
        }else{
            return new ServiceVO(200,"请求失败");
        }

    }

}
